package com.llgz.hotelmanageapi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llgz.hotelmanageapi.common.CommonResult;
import com.llgz.hotelmanageapi.pojo.DAO.RoomReserveInfoDAO;
import com.llgz.hotelmanageapi.pojo.DO.*;
import com.llgz.hotelmanageapi.pojo.DO.RoomReserveInfo;
import com.llgz.hotelmanageapi.service.impl.OrderInfoServiceImpl;
import com.llgz.hotelmanageapi.service.impl.RoomInfoServiceImpl;
import com.llgz.hotelmanageapi.service.impl.RoomReserveInfoServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客房预定信息 前端控制器
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/roomReserveInfo")
@RequiresRoles(value = {"root", "admin", "manager", "reception"}, logical = Logical.OR)
public class RoomReserveInfoController {
    @Autowired
    RoomReserveInfoServiceImpl roomReserveInfoService;
    @Autowired
    OrderInfoServiceImpl orderInfoService;
    @Autowired
    RoomInfoServiceImpl roomInfoService;


    /**
     * @MethodName addRoomReserveInfo
     * @Params * @param RoomReserveInfo
     * @Description 添加
     * @Return CommonResult
     */
    @PostMapping("/addRoomReserveInfo")
    @ApiOperation(value = "添加", response = CommonResult.class, httpMethod = "POST")
    public CommonResult addRoomReserveInfo(@RequestBody RoomReserveInfoDAO roomReserveInfoDAO){
//        OrderInfo orderInfo = new OrderInfo();
//        BeanUtils.copyProperties(roomReserveInfoDAO,orderInfo);
//        System.out.println(orderInfo);
//        RoomReserveInfo reserveInfo = new RoomReserveInfo();
//        BeanUtils.copyProperties(roomReserveInfoDAO,reserveInfo);
//        orderInfoService.save(orderInfo);
//        reserveInfo.setOrderId(orderInfo.getId());
//        boolean save = roomReserveInfoService.save(reserveInfo);
//        boolean save = roomReserveInfoService.save(roomReserveInfo);
        Boolean aBoolean = roomReserveInfoService.addRoomReserve(roomReserveInfoDAO);
        return CommonResult.successResponse(aBoolean);
    }
    /**
     * @MethodName getRoomReserveInfoList
     * @Params * @param RoomReserveInfo
     * @Description 查找全部
     * @Return CommonResult
     */
    @GetMapping("/getRoomReserveInfoList")
    @ApiOperation(value = "查找", response = CommonResult.class, httpMethod = "GET")
    public CommonResult getRoomReserveInfoList(@RequestParam(required = false) String query,
                                               @RequestParam(required = false) String reserveStatus,
                                               @RequestParam Integer pageNum,@RequestParam Integer pageCount){
        QueryWrapper<RoomReserveInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("modified_time");
        if (query != null && !query.isEmpty()){
            queryWrapper.like("reserve_person",query);
        }
        queryWrapper.eq(!StringUtils.isEmpty(reserveStatus),"reserve_status",reserveStatus);
        IPage<RoomReserveInfo> page = roomReserveInfoService.page(new Page<>(pageNum, pageCount), queryWrapper);
        return CommonResult.successResponse(page);
    }
    @GetMapping("/getRoomReserveInfoById")
    @ApiOperation(value = "查找(Id)", response = CommonResult.class, httpMethod = "GET")
    public CommonResult getRoomReserveInfoById(@RequestParam String id){
        RoomReserveInfo reserveInfo = roomReserveInfoService.getOne(new QueryWrapper<RoomReserveInfo>().eq("id", id));
        OrderInfo orderInfo = orderInfoService.getOne(new QueryWrapper<OrderInfo>().eq("id", reserveInfo.getOrderId()));
        RoomInfo roomInfo = roomInfoService.getOne(new QueryWrapper<RoomInfo>().eq("room_id", orderInfo.getRoomId()));
        HashMap<String, Object> map = new HashMap<>();
        map.put("reserveInfo",reserveInfo);
        map.put("orderInfo",orderInfo);
        map.put("roomInfo",roomInfo);
        return CommonResult.successResponse(map);
    }
    /**
     * @MethodName updateRoomReserveInfo
     * @Params * @param RoomReserveInfo
     * @Description 更新
     * @Return CommonResult
     */
    @PostMapping("/updateRoomReserveInfo")
    @ApiOperation(value = "更新", response = CommonResult.class, httpMethod = "POST")
    public CommonResult updateRoomReserveInfo (@RequestBody RoomReserveInfo roomReserveInfo){
        boolean b = roomReserveInfoService.updateById(roomReserveInfo);
        return CommonResult.successResponse(b);
    }
//    /**
//     * @MethodName deleteRoomReserveInfo
//     * @Params * @param RoomReserveInfo
//     * @Description 删除
//     * @Return CommonResult
//     */
//    @PostMapping("/deleteRoomReserveInfo")
//    @ApiOperation(value = "删除", response = CommonResult.class, httpMethod = "POST")
//    public CommonResult deleteRoomReserveInfo (@RequestParam String id){
//        boolean b = roomReserveInfoService.removeById(id);
//        return CommonResult.successResponse(b);
//    }
    /**
     * @MethodName deleteRoomReserveInfo
     * @Params * @param RoomReserveInfo
     * @Description 删除（多选）
     * @Return CommonResult
     */
    @PostMapping("/deleteRoomReserveInfo")
    @ApiOperation(value = "删除（多选）", response = CommonResult.class, httpMethod = "POST")
    public CommonResult deleteRoomReserveInfo (@RequestBody List<String> ids){
        boolean b = roomReserveInfoService.removeByIds(ids);
        return CommonResult.successResponse(b);
    }

    @PostMapping("/cancelReserve")
    @ApiOperation(value = "取消预定", response = CommonResult.class, httpMethod = "POST")
    public CommonResult cancelReserve(@RequestBody RoomReserveInfo roomReserveInfo){
        Boolean reserve = roomReserveInfoService.cancelReserve(roomReserveInfo);
        return CommonResult.successResponse(reserve);
    }

}

